package com.tianquan.miracle.proxy;/**
 * @author starsea
 * @date 2024-10-09 下午8:39
 */


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @BelongsProject: uploaddemo
 * @BelongsPackage: com.tianquan.miracle.proxy
 * @Author: tianquan
 * @CreateTime: 2024-10-09  20:39
 * @Description: 代理工厂
 * @Version: 1.0
 */
public class PoxyFactory {

    public static Object getProxy(Object object, List<String> objectMethodName, Method beforeMethod,
                                  Method aroundMethod, Method afterMethod, Object target) {
        try {
            return ((MethodInvocation) getProxyType(object)
                    .newInstance(object, objectMethodName, beforeMethod, aroundMethod, afterMethod, target))
                    .getProxy();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    private static Constructor getProxyType(Object object) {
        // 判断需要使用什么代理模式
        if (object.getClass().getInterfaces().length >= 1) {
            // jdk
            try {
                return JdkMethodInvocation.class.getConstructor(Object.class, List.class, Method.class,
                        Method.class, Method.class, Object.class);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        } else {
            // cglib
            try {
                return CglibMethodInvocation.class.getConstructor(Object.class, List.class, Method.class,
                        Method.class, Method.class, Object.class);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
    }


}




